<template>
  <h4>当前的求和为：{{ sum }}</h4>
  <button @click="sum++">点我x加一</button>
  <hr>
  <h2>姓名：{{ name }}</h2>
  <h2>年龄：{{ age }}</h2>
  <h2>薪资：{{ job.j1.salary }}</h2>
  <h3 v-show="person.car">座驾信息：{{ person.car }}</h3>
  <button @click="name += '~'">修改姓名</button>
  <button @click="age ++ ">增长年龄</button>
  <button @click="job.j1.salary ++ ">张薪</button>
  <button @click="showRawPerson">输出最原始的person</button>
  <button @click="addCar">给人添加一台车</button>
  <button @click="person.car.name += '@'" v-show="person.car">换车名</button>
  <button @click="person.car.price += 'w'" v-show="person.car"> 换车价</button>
</template>

<script>
import {ref, reactive, toRefs} from 'vue'
import {toRaw} from 'vue'
import {markRaw} from 'vue'

export default {

  setup() {
    // 数据
    let sum = ref(0)
    let person = reactive({
      name: 'achao',
      age: 18,
      job: {
        j1: {
          salary: 100
        }
      }
    })

    function showRawPerson() {
      const p = toRaw(person)
      p.age++
      console.log(p)
    }

    function addCar() {
      let car = {name: '奔驰', price: '40w'}
      person.car = markRaw(car)
    }

    // 返回一个对象
    return {
      sum,
      person,
      ...toRefs(person),
      showRawPerson,
      addCar
    }
  }
}
</script>

<style scoped>

</style>
